/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
import com.inspur.edp.udt.entity.IUdtData;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class BaseTypePropGenerator extends ClassPropertyGenerator {

  protected MediatePropertyInfo privatePropertyInfo;
  protected String propertyFieldName;
  private boolean convertToUpperCase = false;

  public BaseTypePropGenerator(MediatePropertyInfo propertyInfo, String propertyFieldName) {
    this(propertyInfo, propertyFieldName, false);
  }

  public BaseTypePropGenerator(MediatePropertyInfo propertyInfo, String propertyFieldName,
      boolean convertToUpperCase) {
    privatePropertyInfo = propertyInfo;
    this.propertyFieldName = propertyFieldName;
    this.convertToUpperCase = convertToUpperCase;
  }

  protected final MediatePropertyInfo getPropertyInfo() {
    return privatePropertyInfo;
  }

  @Override
  protected TypeInfo getPropertyType() {
    if (privatePropertyInfo.getReturnType().getTypeName().equals("byte[]")) {
      TypeInfo typeInfo = new TypeInfo("byte");
      typeInfo.setArray(true);
      return typeInfo;
    }

    TypeInfo info = privatePropertyInfo.getReturnType();
//    info.setTypePackageName(privatePropertyInfo.getReturnType().getTypePackageName());
//		if (privatePropertyInfo.getElement().getIsUdt()) {
//			info.setUseFullName(true);
//		}
    return info;
  }

  @Override
  protected boolean isGetterOverride() {
    return true;
  }

  private boolean isSetter = true;

  public void setSetterOverride(boolean setter) {
    isSetter = setter;
  }

  @Override
  protected boolean isSetterOverride() {
    return isSetter;
  }

  private boolean hasSetter = true;

  public void setHasSetter(boolean setter) {
    hasSetter = setter;
  }
  @Override
  protected boolean hasSetMethod() {
    return hasSetter;
  }

  @Override
  protected String getFieldName() {
    return propertyFieldName;
  }

  @Override
  protected final String getPropertyName() {
    if (convertToUpperCase) {
      return org.springframework.util.StringUtils.capitalize(privatePropertyInfo.getPropertyName());
    }
    return privatePropertyInfo.getPropertyName();
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getSetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected Block buildGetMethodBody() {
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName(propertyFieldName));
    block.statements().add(returnStatement);
    return block;
  }

  @Override
  protected Block buildSetMethodBody() {
    Block block = ast.newBlock();
    Assignment assignment = ast.newAssignment();
    assignment.setLeftHandSide(ast.newSimpleName(propertyFieldName));
    assignment.setOperator(Assignment.Operator.ASSIGN);
    assignment.setRightHandSide(ast.newSimpleName("value"));
    ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);
    block.statements().add(expressionStatement);
    return block;
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    ArrayList<AnnotationInfo> list = super.getGetterAttributeList();
    if (list == null) {
      list = new ArrayList<>();
    }

    if (convertToUpperCase) {
      AnnotationInfo tempVar = new AnnotationInfo();
      tempVar.setAnnotationType(AnnotationType.Marker);
      tempVar.setTypeInfo(new TypeInfo(JsonIgnore.class));
      list.add(tempVar);
			return list;
		}

    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    annotationInfo.setTypeInfo(new TypeInfo(JsonProperty.class));
    annotationInfo.setSingleParam(ExpressionUtils.getStringLiteral(
        ast, StringUtils.toCamelCase(getPropertyName())
    ));
    list.add(annotationInfo);
    return list;
  }

  @Override
  protected ArrayList<AnnotationInfo> getSetterAttributeList() {
		if (!convertToUpperCase) {
			return null;
		}
    ArrayList<AnnotationInfo> list = super.getSetterAttributeList();
    if (list == null) {
      list = new ArrayList<>();
    }
    AnnotationInfo info = new AnnotationInfo();
    info.setAnnotationType(AnnotationType.Marker);
    info.setTypeInfo(new TypeInfo(JsonIgnore.class));
    list.add(info);
    return list;
  }


}
